package com.example.mvvm_base.view;

import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.ViewModelProvider;

import com.example.mvvm_base.base.viewmodel.BaseViewModel;
import com.example.mvvm_base.event.CustomEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public abstract class BaseView<VDB extends ViewDataBinding,VM extends BaseViewModel> extends AppCompatActivity {
    protected VDB viewDataBind;
    protected VM viewModel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewDataBind= DataBindingUtil.setContentView(this,getLayoutId());
        viewModel= new ViewModelProvider(this).get(getViewModel());
        initControlView();
        initData();
        EventBus.getDefault().register(this);
//        getWindow().setStatusBarColor(Color.WHITE);

    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onReceiver(CustomEvent event){
        if(event==null&&event.getTarget()==null)return;
        if(event.getTarget()==getClass().getSimpleName()){
            handEvent(event);
        }

    }

    public void handEvent(CustomEvent event) {

    }

    protected abstract void initData();

    protected abstract void initControlView();

    public  abstract int  getLayoutId();
    public  abstract Class<VM>  getViewModel();
}
